package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import serivce.LoginSerivceImp;
import serivce.LoginService;
import serivce.UserSerivce;
import untity.User;


import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
public class Login {

    @Autowired
    private UserSerivce userSerivce;

    @Autowired
    private LoginService loginSerivce;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject loginAxios(@RequestBody String data, HttpServletRequest request){
        JSONObject jsonObject= JSON.parseObject(data);
        System.out.println(jsonObject);
        String account=jsonObject.getString("account");
        String password=jsonObject.getString("password");
        System.out.println(data);
        System.out.println("账号"+account+"密码"+password);
        HashMap<String,Object> hashMap=loginSerivce.login(account,password);
        if (hashMap.isEmpty()){
            return null;
        }
        String id=hashMap.get("id").toString();
        String userName=hashMap.get("userName").toString();
        JSONObject json=new JSONObject(hashMap);
        System.out.println("id="+id);
        System.out.println(json);
        if (id!=null){
            loginSerivce.setLoginSession(request,id,userName);

        }

        return json;
    }

    @ResponseBody
    @RequestMapping("/register")
    public String register(HttpServletRequest request,@RequestBody JSONObject data){
        String account=data.getString("account");
        String password=data.getString("password");


//        System.out.println(account);
//        System.out.println(password);
//        System.out.println(data);
        String flag=loginSerivce.queryByAccount(account);
        if (flag.equals("0")){
            User user=new User();
            user.setUserName(account);
            user.setUserPassword(password);
            loginSerivce.addUser(user);
            System.out.println(user);
            String id= String.valueOf(user.getId());
            userSerivce.creatHistory(id);
            return "1";
        }else {
            return "0";
        }

    }

    @ResponseBody
    @PostMapping("/check")
    public HashMap<String, Object> checkUser(HttpServletRequest request){
        HashMap<String,Object> hashMap=loginSerivce.checkSession(request);
        return hashMap;
    }

//    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
//    public String doLogin(HttpServletResponse res,String userName,String password) throws IOException {
//        System.out.println("doLogin");
//        PrintWriter pw=res.getWriter();
////        redirect(重定向):映射路径
//        return "redirect:/index";
//    }
//
//    @RequestMapping(value = "/test",method = RequestMethod.POST)
//    public String test(HttpServletResponse res,String userName,String age) throws IOException {
//        System.out.println("test");
//        res.setContentType("text/html;charset=utf-8");
//        res.getWriter();
//        return "index";
//    }
//
//    @ResponseBody
//    @RequestMapping(value = "/axios",method = RequestMethod.POST)
//    public Student axios(){
//        Student s=new Student();
//        System.out.println("axios");
//        s.setName("leijica");
//        s.setAge("123");
//        return s;
//    }


}
